var mongoose = require("mongoose")

mongoose
    .connect("mongodb://127.0.0.1:27017/mpone")
    .then((res)=> {
        console.log("成功");
})
    .catch((err) =>{
        console.log("失败");

});

var Schema = mongoose.Schema

var ArticleSchema = new Schema(
    {
        title: String,
        content: String,
        author: String,
        tag: String,
        views:{
            type: Number,
            default: 0,
        }

    },
    {
        timestamps: true,
    }
)

var Article = mongoose.model("Article", ArticleSchema)


Article.create([{
    title:'日常',
    content:'太阳很好',
    author:' AZ',
    tag:'生活',
    },{
        title:'五月',
        content:'荷花开了',
        author:' AZ',
        tag:'生活',
    },
]).then((res) => {
    console.log(res);
    console.log("创建成功，并打印");
})
    
var first  = new Article({
    title:'下雨天',
    content:'你在马路边',
    author:'大写',
    tag:'日常',
})

first.save().then( r => {
    console.log(r);
    console.log('成功');
})


Article.deleteOne({_id:"67669b016c0cf82c70768643"})
    .then((r) => {
        console.log(r);
    })
    .catch((err) => {
        console.log(err);
    })


Article.deleteMany({tag:/生活/})
    .then((r) => {
        console.log(r);
    })
    .catch((err) => {
        console.log(err);
    })



Article.updateOne({_id:"6766b102c1bb0ad7989b4cb2"},
    {
        title:"Mee 雾化",
        author:"饼干",

}).then((r) => {
    console.log(r);
})


Article.updateOne(
    {
        _id:"67669dda3ac2664c7831a611",
    },
    {
        $inc:{ views: 1},
    },
    {
        timestamps: false,
    }

).then((r) => {
    console.log(r);
})



Article.findById("6766b102c1bb0ad7989b4cb2").then((r) => {
    console.log(r);
})

console.log("---------------------------")

Article.findOne({_id:"67669dda3ac2664c7831a611"}).then((r) => {
    console.log(r);
})


Article.findByIdAndUpdate("6766b102c1bb0ad7989b4cb2",
    {$inc:{ views: 100}},
        { timestamps: false}
).then((r) => {
    console.log(r);
})

Article.findById("6766b102c1bb0ad7989b4cb2")
.then((r) => {
    console.log(r);
})


Article.find({views:{ $gte:0, $lte:1000}})
    .sort({_id: -1})
    .skip(0)
    .limit(3)
    .select({ views: 0 , author: 0})
    .exec()
    .then((r) => {
        console.log(r);
    })









